'''
基础层：封装元素定位和操作；
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(message)s')


class BasePage():
    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        """实现：打开 url 页面"""
        self.driver.get(url)
        self.driver.maximize_window()

    def find_ele(self, loc):
        """实现：元素定位"""
        ele = self.driver.find_element(*loc)
        return ele
        logging.info(f'定位{loc}成功')

    def input_ele(self, loc, value):
        """实现：输入框输入数据"""
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}输入{value}成功')

    def ele_click(self, loc):
        """实现：按钮点击"""
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'点击{loc}成功')

    def ele_clear(self, loc):
        """实现：输入框清空"""
        ele = self.find_ele(loc)
        ele.clear()
        logging.info(f'在{loc}清除文本成功')


# 测试代码
if __name__ == '__main__':
    url = r'https://www.baidu.com/'
    kw_loc = (By.ID, 'kw')
    su_loc = (By.ID, 'su')
    value = 'selenium'

    chrome_location = r'D:\software\Win_x64_1135105_chrome-win\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    base_page = BasePage(driver)
    base_page.get_url(url)
    base_page.ele_clear(kw_loc)
    base_page.input_ele(kw_loc, value)
    base_page.ele_click(su_loc)
    sleep(3)

    driver.quit()
